//
//  ZYCNoticePeopleManager.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/30.
//

#import "ZYCNoticePeopleManager.h"
#import "ZYCRoleTreeModel.h"

@implementation ZYCNoticePeopleManager


static ZYCNoticePeopleManager *_instancetype = nil;

+ (instancetype)sharedManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instancetype = [[ZYCNoticePeopleManager alloc]init];
    });
    return _instancetype;
}

- (NSArray *)filterSource:(NSArray *)sourceArray type:(NSString *)type{
   if (sourceArray.count == 0)  return nil;
   NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
   for (ZYCRoleTreeModel *model in sourceArray) {
       if ([model.dateType isEqualToString:type]) {
           [mArr addObject:model];
       }
       NSArray *temp = [self filterSource:model.children type:type];
       if (temp.count > 0) {
           [mArr addObjectsFromArray:temp];
       }
   }
   return mArr.copy;
}

- (NSArray *)childrenArr:(NSArray<ZYCRoleTreeModel *> *)children select:(BOOL)select {
    if (!children)  return nil;
    NSMutableArray *totalArray = [NSMutableArray arrayWithCapacity:0];
    for (ZYCRoleTreeModel *model in children) {
        NSMutableArray *sonArray = [NSMutableArray arrayWithCapacity:0];
        model.isSelected = select;
        [sonArray addObjectsFromArray:[self childrenArr:model.children select:select]];
        model.selectArr = sonArray.copy;
        if (select && [model.dateType integerValue] == 4) {
            [totalArray addObject:model];
        }
        [totalArray addObjectsFromArray:sonArray];
    }
    return totalArray.copy;
}


- (void)fatherSelectArr:(ZYCRoleTreeModel *)father {
    if (!father) {
        return;
    }
    BOOL isSelected = YES;
    NSMutableArray *selectArr = [NSMutableArray arrayWithCapacity:0];
    for (ZYCRoleTreeModel *child in father.children) {
        if ([child.dateType integerValue] == 4) {
            if (child.isSelected) {
                [selectArr addObject:child];
            }
            else {
                isSelected = NO;
            }
        }
        else {
            [selectArr addObjectsFromArray:child.selectArr];
            if (child.isSelected == NO) {
                isSelected = NO;
            }
        }
    }
    father.isSelected = isSelected;
    father.selectArr = selectArr.copy;
    //递归
    [self fatherSelectArr:father.father];

}


@end
